<?php


namespace app\api\controller;


use think\Collection;
use think\Db;
use think\Exception;

class Commondevice extends Collection
{

    //protected $publicurl = "http://quick2.shenzhuo.vip:11360/";
    protected $publicurl = "http://control.unionstart.work:8090/";

    function imgToBaes64($url)
    {
        $imgInfo = getimagesize($url);
        return base64_encode(file_get_contents($url));
        //return 'data:'.$imgInfo['mime'].';base64,'.base64_encode(file_get_contents($url));
    }

    function newtext($params){
        $myfile = fopen(__DIR__."/newfile.txt", "a") or die("Unable to open file!");//这个是在c盘根目录生成文件
        $txt = $params;//获取参数
        $b="";
        foreach($txt as $k=>$v){
            $b = $b ."[".$k."]=>".$v."\n";
        }
        fwrite($myfile, $b);//写入内容,可以写多次哦,不过没啥意义,因为你拼接好字符串,一次写入就行了
        fclose($myfile);//关闭该操作
    }


    function newtext1($params){
        $myfile = fopen(__DIR__."/newfile1.txt", "a") or die("Unable to open file!");//这个是在c盘根目录生成文件
        $txt = $params;//获取参数

        fwrite($myfile, $txt);//写入内容,可以写多次哦,不过没啥意义,因为你拼接好字符串,一次写入就行了
        fclose($myfile);//关闭该操作
    }

    public function cartDel1(){
        $employeeNo = "YQ000120210129U95898";
//        $url = "http://quick2.shenzhuo.vip:11360/card/del/".$employeeNo;
        $url = $this->publicurl."/card/del/".$employeeNo;
        $post_data = [];
        $black = $this->docurl($url,$post_data,1);
        $black = json_decode($black,true);
        dump($black);die;
        if($black['code']!=200){
            throw new Exception($black['msg']);
        }
        return true;
    }




    public function cardUpdate1(){
        $post_data['employeeNo'] = 'YQ000120210129U95898';
        $post_data['faceImageBase64'] = "https://tickets-1253901423.cos.ap-nanjing.myqcloud.com/platform1/2021-02-03/79882db2dc322e5184f162885f04c5e8.jpg";
        if(!empty($post_data['faceImageBase64'])){
            $base64 = $this->imgToBaes64($post_data['faceImageBase64']);
            $post_data['faceImageBase64'] = $base64;
        }
        $url = $this->publicurl."/card/update";
        $post_data = json_encode($post_data);
        $black = $this->docurl($url,$post_data);
        $black = json_decode($black,true);
        dump($black);die;
        if($black['code']!=200){
            throw new Exception($black['msg']);
        }
        return true;
    }

    public function cardAdd1(){

//$post_data
//        $img = "https://tickets-1253901423.cos.ap-nanjing.myqcloud.com/platform1/2021-02-01/dfdaf759eda0f12aa69cf608cbb3fd77.jpg";
//        $post_data['faceImageBase64'] = "https://tickets-1253901423.cos.ap-nanjing.myqcloud.com/platform1/2021-02-01/257fb34738e21354a705b7608c2822a3.jpg"; 支雪冬
//        $post_data['faceImageBase64'] = "https://tickets-1253901423.cos.ap-nanjing.myqcloud.com/platform1/2021-02-03/79882db2dc322e5184f162885f04c5e8.jpg";
        $post_data['faceImageBase64'] = "https://tickets-1253901423.cos.ap-nanjing.myqcloud.com/platform1/2021-02-03/09420a8c97cf503b45fe747331c229d2.jpg";
//        $base64 = $this->imgToBaes64($img);
        $post_data['employeeNo'] = "YQ0001202101311199504231a3";
//        $this->cartDel($post_data['employeeNo']);
        $post_data['cardNos'] = ['AI0001040003708eba6f','42058219921108359X'];
        $post_data['personName'] = "支雪冬";
//        $post_data['faceImageBase64'] = $base64;
        //$this->newtext1($post_data);
        if(!empty($post_data['faceImageBase64'])){
            $base64 = $this->imgToBaes64($post_data['faceImageBase64']);
            $post_data['faceImageBase64'] = $base64;
        }
        $url = $this->publicurl."/card/add";
        $post_data = json_encode($post_data);
        $black = $this->docurl($url,$post_data);
        $black = json_decode($black,true);
        dump($black);die;
        if($black['code']!=200){
            throw new Exception($black['msg']);
        }
        return true;
    }

    /**
     * 注册会员  employeeNo  idCardNo  personName  faceImageBase64
     * @author zhixuedong
     * @date 2021/2/1/001 $post_data
     */
    public function cardAdd($post_data){
        //$post_data['faceImageBase64'] = "https://tickets-1253901423.cos.ap-nanjing.myqcloud.com/platform1/2021-02-03/09420a8c97cf503b45fe747331c229d2.jpg";
        if(!empty($post_data['faceImageBase64'])){
            $base64 = $post_data['faceImageBase64'];
            unset($post_data['faceImageBase64']);
            $update_data['faceImageBase64'] = $base64;
            $update_data['employeeNo'] = $post_data['employeeNo'];
        }
        $employeeNo = $post_data['employeeNo'];
        $url = $this->publicurl."/card/add";
        $post_data = json_encode($post_data);
        $black = $this->docurl($url,$post_data);
        $black = json_decode($black,true);
        if($black['code']!=200){
            return $black;
        }else{
            if(!empty($update_data)){
                $this->cardUpdate($update_data);
            }
            Db::name("tickets_list")->where(['signcode'=>$employeeNo])->update(['status'=>1]);
        }
        return true;
    }


    /**
     * 删除人员
     * @param $employeeNo
     * @return bool
     * @throws Exception
     * @author zhixuedong
     * @date 2021/2/1/001
     */
    public function cartDel($employeeNo){
//        $employeeNo = "AI000120210201q289500fe0d2";
//        $url = "http://quick2.shenzhuo.vip:11360/card/del/".$employeeNo;
        $url = $this->publicurl."/card/del/".$employeeNo;
        $post_data = [];
        $black = $this->docurl($url,$post_data,1);
        $black = json_decode($black,true);
        if($black['code']!=200){
            return $black;
        }
        return true;
    }

    /**
     * 更新人脸
     * @return bool
     * @throws Exception
     * @author zhixuedong
     * @date 2021/2/1/001
     */
    public function cardUpdate($post_data){
//        $post_data['employeeNo'] = 'YQ000120210131Y3874968ad27';
//        $post_data['faceImageBase64'] = 'YQ000120210131Y3874968ad27';
        if(!empty($post_data['faceImageBase64'])){
            $base64 = $this->imgToBaes64($post_data['faceImageBase64']);
            $post_data['faceImageBase64'] = $base64;
        }
        $url = $this->publicurl."/card/update";
        $post_data = json_encode($post_data);
        $black = $this->docurl($url,$post_data);
        $black = json_decode($black,true);
        if($black['code']!=200){
            return $black;
        }
        return true;
    }





    /**
     * 临时卡上报
     * @author zhixuedong
     * @date 2021/2/1/001
     */
    public function cardQrcode(){
        $post_data['code'] = 'YQ000120210131g12338c2c78d';
        $post_data['serial'] = 'F22741861';
        $url = $this->publicurl."/card/qrcode";
        $post_data = json_encode($post_data);
        $black = $this->docurl($url,$post_data);
        $black = json_decode($black,true);
        if($black['code']!=200){
            return $black;
        }
        return true;
    }

    /**
     * 注册设备
     * @return bool
     * @throws Exception
     * @author zhixuedong
     * @date 2021/2/1/001 $post_data
     */
    public function deviceAdd($post_data){
//        $post_data['deviceSerial'] = 'F22741861';
//        $post_data['qrcodeSerial'] = 'E05426006';
        $post_data['validateCode'] = empty($post_data['validateCode'])?'TDZDGW':$post_data['validateCode'];
        $url = $this->publicurl."/device/add";
        $post_data = json_encode($post_data);
        $black = $this->docurl($url,$post_data);
        $black = json_decode($black,true);
        if($black['code']!=200){
            return $black;
        }
        return true;
    }


    /**
     * 删除设备
     * @param $employeeNo
     * @return bool
     * @throws Exception
     * @author zhixuedong
     * @date 2021/2/1/001 $deviceSerial
     */
    public function deviceDel($deviceSerial){
//        $deviceSerial = "F22741861";
        $url = $this->publicurl."/device/del/".$deviceSerial;
        $post_data = [];
        $black = $this->docurl($url,$post_data,1);
        $black = json_decode($black,true);
        if($black['code']!=200){
            return $black;
        }
        return true;
    }

    /**
     * 设备开门
     * @return bool
     * @throws Exception
     * @author zhixuedong
     * @date 2021/2/1/001
     */
    public function deviceOpen(){
        $deviceSerial = "F22741861";
        $url = $this->publicurl."/device/open/".$deviceSerial;
        $post_data = [];
        $black = $this->docurl($url,$post_data,2);
        $black = json_decode($black,true);
        if($black['code']!=200){
            return $black;
        }
        return true;
    }

    /**
     * 请求操作
     * @param $url
     * @param $post_data
     * @author zhixuedong
     * @date 2021/2/1/001
     */
    public function docurl($url,$post_data,$type=0){
        switch ($type){
            case 0:
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                        'Content-Type: application/json; charset=utf-8',
                        'Content-Length: ' . strlen($post_data)
                    )
                );
                break;
            case 1:
                $ch = curl_init($url);
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
                curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                break;
            case 2:
                $ch = curl_init($url);
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
                curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                break;
        }
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $response;
    }



}